package edu.maplef.bookwild.dao.impl.jdo;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.Link;
import com.google.gdata.data.douban.Attribute;
import com.google.gdata.data.douban.SubjectEntry;
import com.google.gdata.util.ServiceException;

import edu.maplef.bookwild.config.Config;
import edu.maplef.bookwild.schema.Book;
import edu.maplef.bookwild.utils.DoubanServiceUtil;

public class DaoJDOImplTest {

	private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
			new LocalDatastoreServiceTestConfig());
	private final LocalServiceTestHelper helper2 = new LocalServiceTestHelper(
			new LocalMemcacheServiceTestConfig());

	private BookDaoJDOImpl bd = new BookDaoJDOImpl();
	private DoubanService dbs = null;

	@Before
	public void setUp() throws Exception {
		helper.setUp();
		helper2.setUp();
		if (!Config.isInit)
			Config.init();
		dbs = DoubanServiceUtil.getDoubanService();
	}

	@After
	public void tearDown() throws Exception {
		helper.tearDown();
		helper2.tearDown();
	}

	@Test
	public void test() throws Exception {
		TestList();
		// TestFindByIds();
	}

	public void TestEntry2Book() throws Exception {
		try {
			SubjectEntry entity = dbs.getBook(1123456);
			Book b = bd.entry2Book(entity);
			System.out.println(b.getTitle());
			for (Attribute attr : entity.getAttributes()) {
				System.out.println(attr.getName() + " : " + attr.getContent());
			}
			for (Link l : entity.getLinks()) {
				System.out.println(l.getRel() + "-" + l.getHref());
			}
			//调用dao，保存
			System.out.println(bd.save(b));
			System.out.println(bd.getById(b.getId()));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void TestFindByIds() throws Exception {

		SubjectEntry entity = dbs.getBook(1123456);
		Book b = bd.entry2Book(entity);
		System.out.println(b.getTitle());
		System.out.println(bd.save(b));

		SubjectEntry entity2 = dbs.getBook(5391812);
		Book b2 = bd.entry2Book(entity2);
		System.out.println(b2.getTitle());
		System.out.println(bd.save(b2));

		String[] ids = new String[] { b.getId(), b2.getId() };
		System.out.println(bd.getById(b.getId()));
		for (Book k : bd.getByIds(ids)) {
			System.out.println(k.getTitle());
		}
		bd.removeById(b.getId());
		bd.getById(b.getId());
	}

	public void TestList() throws Exception {

		SubjectEntry entity = dbs.getBook(1123456);
		Book b = bd.entry2Book(entity);
		System.out.println(b.getTitle());
		System.out.println(bd.save(b));

		SubjectEntry entity2 = dbs
				.getBook("http://api.douban.com/book/subject/5391812");
		Book b2 = bd.entry2Book(entity2);
		System.out.println(b2.getTitle());
		System.out.println(bd.save(b2));

		String[] ids = new String[] { b.getId(), b2.getId() };
		System.out.println(bd.removeByIds(ids));
		for (Book bl : bd.list()) {
			System.out.println(bl.getTitle());
		}
	}

}